package com.jintoufs.weixin.address;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.address.Address;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.address.AddressService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.weixin.SupportAction;
import io.swagger.annotations.Api;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value = "地址信息相关接口", description = "地址信息相关接口")
@RestController
@RequestMapping("/api/address")
public class AddressAction extends SupportAction {
    @Resource
    private AddressService addressService;

    @GetMapping("/oper")
    public AjaxMsg oper(String address,String token,String type){
        Address en= JSON.parseObject(address,Address.class);
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {

            switch (type){
                case "add":
                    Map<String,Object> param = new HashMap<>();
                    param.put("userId",customer.getId());
                    param.put("isDefault",true);
                    List<Address> addressList = addressService.selectByParam(param);
                    if (addressList.size()==0){
                        en.setIsDefault(true);
                    }else {
                        if (en.getIsDefault()){
                            addressList.get(0).setIsDefault(false);
                            addressService.save(addressList.get(0),null);
                        }
                    }
                    en.setUserId(customer.getId());
                    addressService.save(en,getUUID());

                    break;
                case "edit":
                    Map<String,Object> param2 = new HashMap<>();
                    param2.put("userId",customer.getId());
                    param2.put("isDefault",true);
                    List<Address> addressList2 = addressService.selectByParam(param2);
                    if (addressList2.size()==0){
                        en.setIsDefault(true);
                    }else {
                        if (en.getIsDefault()){
                            addressList2.get(0).setIsDefault(false);
                            addressService.save(addressList2.get(0),null);
                        }
                    }
                    en.setUserId(customer.getId());
                    addressService.save(en,null);
                    break;
                case "del":
                    addressService.delete(en.getId());
                    break;
                default:
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("未知操作。");
                    break;
            }
        }
        return ajaxMsg;
    }

    @GetMapping("/getDefaultAddress")
    public AjaxMsg getDefaultAddress(String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {
            Map<String,Object> param = new HashMap<>();
            param.put("userId",customer.getId());
            param.put("isDefault","1");
            try {
                ajaxMsg.setDatas(addressService.selectByParam(param));
            }catch (Exception e){
                e.printStackTrace();
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("传入参数错误");
            }
        }
        return ajaxMsg;
    }


    @GetMapping("/serach")
    public AjaxMsg serach(String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {
            Map<String,Object> param = new HashMap<>();
            param.put("userId",customer.getId());
            try {
                ajaxMsg.setDatas(addressService.selectByParam(param));
            }catch (Exception e){
                e.printStackTrace();
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("传入参数错误");
            }
        }
        return ajaxMsg;
    }
}
